package com.znf4.controller.platform.resource.mechanics;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.znf4.enums.OperationStatusEnum;
import com.znf4.id.IdGenerator;
import com.znf4.platform.resource.mechanics.entity.Mechanics;
import com.znf4.platform.resource.mechanics.server.MechanicsServer;

/**
 * 平台机械资源信息
 */
@Controller
@RequestMapping("/platfrom/mechanics")
public class MechanicsController {
	
	@Autowired
	private MechanicsServer mechanicsServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	

	/**
	 * 函数功能说明 ： 添加机械资源
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public String add(Mechanics mechanics , Model model) {
		
		
		mechanics.setId(idGenerator.getPlatformRresourceMechanicsNumber());
		
		mechanicsServer.addMechanics(mechanics); 
		
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		return "good/list";
	}

	
	
	/**
	 * 函数功能说明 ： 修改机械资源信息
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public String update(Mechanics mechanics , Model model) {
		
		mechanicsServer.updateMechanics(mechanics);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除机械资源信息
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public String delOne(Mechanics mechanics , Model model) {
		
		mechanicsServer.delOneMechanics(mechanics);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除多条机械资源信息
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String delAll(String[] mechanicss  , Model model) {
		
		List<Mechanics> list = new ArrayList<>() ;
		for(int i=0 ; i < mechanicss.length ; i ++ ){
			Mechanics mechanics = new Mechanics() ;
			mechanics.setId(mechanicss[i]);
			list.add(mechanics) ;
		}
		mechanicsServer.delAllMechanics(list);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		return "good/list";
	}
	

	/**
	 * 函数功能说明 ： 获取机械资源
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String queryAll(Mechanics mechanics , Model model) {
		
		List<Mechanics> list = mechanicsServer.queryAll(mechanics);
		model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc() );
		model.addAttribute("data", list );
		return "good/list";
	}
	
	

}
